#if !defined editheadfile 
#define editheadfile 




void edit(char*fn); 
void hint(int n); 
int insert(int num,char length); 
void delete(); 
void deleteall(); 
void printnotes(); 
void printnote(int tnplc); 
void editnotes(); 
void updt(int tnplc,int num,char length); 
void mark(); 
void updtins(); 
int inputfn(char*fn,int w); 
void printfn(char*fn,int n,int w); 
int loadnotes(char*fn); 
int savenotes(char*fn); 
void initnotes() 
{ 
int n; 
for(n=0;n<601;n++) 
notes[n].num=-1,notes[n].length='z'; 
N=0,nplc=0,modi=0; 
} 





/*void main(void) 
{ 
int gd=DETECT,gm,n; 
struct note*start=(struct note*)malloc(sizeof(struct note)); 
start->fp=0,start->pp=0; 
current=start; 
initgraph(&gd,&gm,""); 
edit(start); 
closegraph(); 
}*/ 



void edit(char*fn) 
{ 
int color[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int issave=0; 
setfillstyle(1,color[9]); 
bar(0,0,640,480); 
menupad(10,10,629,469,3); 
setcolor(color[15]); 
outtextxy(18,18,"Opern Editer"); 
outtextxy(18,36,"save as"); 
outtextxy(185,36,".opn"); 
setcolor(color[8]); 
outtextxy(17,35,"save as"); 
outtextxy(184,35,".opn"); 
outtextxy(20,450,"Enter"); 
outtextxy(480,35,"Type't'for test!"); 
setfillstyle(1,color[1]); 
bar(15,50,624,436); 
setfillstyle(1,color[2]); 
bar(80,30,180,46); 
buttondn(80,30,180,46);/*filename window*/ 
buttondn(15,50,624,436);/*edit window*/ 
updtins(); 
{ 
int tp=nplc; 
nplc=1; 
printnotes(); 
nplc=tp; 
} 
while(issave==0) 
{ 
editnotes(); 
mark(); 
hint(1); 
issave=inputfn(fn,0); 
if(!issave) 
if(modi) 
{ 
int re=messagebox(200,200,460,280,"return without save?(Y/N)"); 
if((char)re=='y'||(char)re=='Y')break; 
} 
else break; 
} 
if(issave)savenotes(fn); 
} 

void hint(int n) 
{ 
char s[50]; 
switch(n) 
{ 
case 0:sprintf(s,"File Name");break; 
case 1:sprintf(s,"Num To Set Note(8='-');Right/Left To Change Place");break; 
case 2:sprintf(s,"Up/Down To Set Tone;Right/Left To Set Length");break; 
defualt:return; 
} 
setfillstyle(1,color[7]); 
bar(66,450,456,458); 
setcolor(color[8]); 
outtextxy(66,450,s); 
} 
int insert(int num,char length) 
{ 
if(N>=600||N<0||nplc<0||nplc>N)return 0; 
{ 
int n; 
for(n=N;n>nplc;n--) 
{ 
notes[n].num=notes[n-1].num; 
notes[n].length=notes[n-1].length; 
} 
notes[nplc].num=num,notes[nplc].length=length; 
N++; 
nplc++; 
} 
return 1; 
} 
void delete() 
{ 
if(N<0||nplc<0||nplc>N)return; 
if(nplc<=0)return; 
if(nplc<=1&&ins==0)return; 
{ 
int n; 
for(n=nplc-1;n<N;n++) 
{ 
notes[n].num=notes[n+1].num; 
notes[n].length=notes[n+1].length; 
} 
N--,nplc--; 
} 
} 
void printnotes() 
{ 
int tnplc; 
for(tnplc=nplc;tnplc<=N;tnplc++) 
printnote(tnplc); 
setfillstyle(1,color[1]); 
if(tnplc<=600) 
{ 
int l1=(tnplc-1)%50,h1=(tnplc-1)/50,x=20+12*l1,y=65+30*h1; 
bar(x,y,620,y+25); 
bar(20,y+30,620,430); 
} 
} 
void printnote(int tnplc) 
{ 
if(tnplc>N||N>=600)return; 
{ 
char s[2],length=notes[tnplc-1].length; 
int l1=(tnplc-1)%50,h1=(tnplc-1)/50; 
int num=notes[tnplc-1].num,x=20+12*l1,y=65+30*h1; 
if(num==-1)return; 
setfillstyle(1,color[1]); 
bar(x,y,x+10,y+24); 
setcolor(color[2]); 
if(num==8) 
{ 
line(x,y+8,x+8,y+8); 
return; 
} 
else sprintf(s,"%d",num%10); 
outtextxy(x,y+6,s); 
if(num<8&&num>0)outtextxy(x,y+15,"."); 
else if(num>20)outtextxy(x,y-2,"."); 
if(num>30)outtextxy(x,y-5,"."); 
switch(length) 
{ 
case '=':line(x,y+18,x+10,y+18); 
case '-':line(x,y+15,x+10,y+15);break; 
case '>':line(x,y+15,x+10,y+15); 
case '.':outtextxy(x+6,y+6,".");break; 
default:break; 
} 
} 
} 
void editnotes() 
{ 
int key=0,num,tone=0,length=0,isnoteload=0; 
char sl[]={' ','=','-','>','.',' ','='}; 
mark(); 
hint(1); 
while(key!=bios_esc) 
{ 
if(!isnoteload) 
key=bioskey(0); 
isnoteload=0; 
if((char)key>='0'&&(char)key<='8') 
{ 
int isinserted; 
modi=1; 
num=(char)key-48; 
mark(); 
if(ins)isinserted=insert(num,sl[length]); 
if(isinserted||!ins) 
{ 
mark(); 
if(num==0||num==8)updt(nplc,num,sl[length]); 
else updt(nplc,tone+num,sl[length]); 
printnotes(); 
if(num>=0&&num<8) 
{ 
hint(2); 
key=bioskey(0); 
while(1) 
{ 
if(key==bios_up&&tone<=30&&num!=0)tone+=10; 
else if(key==bios_dn&&tone>=10&&num!=0)tone-=10; 
else if(key==bios_right)length=(length+1)%5; 
else if(key==bios_left)length=(length+4)%5; 
else if(key!=bios_up&&key!=bios_dn&&key!=bios_right&&bios_left) 
{ 
isnoteload=1; 
break; 
} 
if(num>5&&tone==30)tone=20; 
if(num==0)updt(nplc,0,sl[length]); 
else updt(nplc,tone+num,sl[length]); 
printnote(nplc); 
key=bioskey(0); 
} 
} 

} 
else mark(); 
hint(1); 
} 
if(key==bios_insert) 
{ 
mark(); 
if(ins&&nplc<N) 
{ 
nplc++; 
ins=0; 
updtins(); 
} 
else if(ins==0&&nplc>0) 
{ 
nplc--; 
ins=1; 
updtins(); 
} 
mark(); 
} 
else if(key==bios_right&&nplc<N) 
{ 
mark(); 
nplc++; 
mark(); 
} 
else if(key==bios_left&&nplc>0&&!(!ins&&nplc==1)) 
{ 
mark(); 
nplc--; 
mark(); 
} 
else if(key==bios_back&&nplc>0) 
{ 
mark(); 
modi=1; 
delete(); 
if(nplc==0) 
{ 
nplc++; 
printnotes(); 
nplc--; 
} 
else printnotes(); 
mark(); 
} 
else if((char)key=='t'||(char)key=='T')bookplay(0); 
} 
} 
void updt(int tnplc,int num,char length) 
{ 
notes[tnplc-1].num=num,notes[tnplc-1].length=length; 
} 
void mark() 
{ 
int l1=nplc%50,h1=nplc/50; 
int x=20+12*l1,y=65+30*h1; 
if(h1==12)x=620,y-=30; 
else if(!ins&&x==20)x=620,y-=30; 
if(ins)opline(x-1,y-3,x-1,y+25); 
else oprect(x-1,y-3,x-13,y+25); 
} 
void updtins() 
{ 
setfillstyle(1,7); 
bar(560,450,625,458); 
setcolor(color[8]); 
if(ins)outtextxy(560,450,">Insert<"); 
} 
int inputfn(char*fn,int w) 
{ 
int key=0,n=strlen(fn),np=n; 
if(!w)hint(0); 
setfillstyle(1,color[2]); 
setcolor(color[8]); 
while(key!=bios_enter&&key!=bios_esc) 
{ 
char c; 
printfn(fn,np,w); 
key=bioskey(0); 
c=(char)key; 
if(n<8) 
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')) 
{ 
int x; 
for(x=n;x>np;x--) 
*(fn+x)=*(fn+x-1); 
*(fn+np)=c,n++,np++; 
} 
if(key==bios_back&&np>0) 
{ 
int x; 
for(x=np-1;x<=n;x++) 
*(fn+x)=*(fn+x+1); 
n--,np--; 
} 
else if(key==bios_right&&np<n) 
{ 
np++; 
} 
else if(key==bios_left&&np>0) 
{ 
np--; 
} 
} 
printfn("",-1,w); 
if(n==0)return 0; 
return key==bios_enter; 
} 
void deleteall() 
{ 
initnotes(); 
} 
void printfn(char*fn,int n,int w) 
{ 
int x=0,y=0; 
if(w)x=200,y=200; 
bar(x+88,y+36,x+160,y+44); 
outtextxy(x+88,y+36,fn); 
if(n!=-1)opline(x+88+n*8,y+36,x+88+n*8,y+44); 
} 
int loadnotes(char*fn) 
{ 
FILE*fp; 
char realfn[15]={0}; 
char*vs; 
int num; 
char length; 
sprintf(realfn,"%s.opn",fn); 
fp=fopen(realfn,"r"); 
if(fp==-1||fp==0)return 0; 
fscanf(fp,"%s ",vs); 
if(strcmp(version,vs)!=0) 
{ 
fclose(fp); 
return 0; 
} 
deleteall(); 
while(!feof(fp)) 
{ 
fscanf(fp,",%d,%c",&num,&length); 
if(feof(fp))break; 
insert(num,length); 
} 
fclose(fp); 
return 1; 
} 
int savenotes(char*fn) 
{ 
FILE*fp; 
int n; 
char realfn[15]={0}; 
sprintf(realfn,"%s.opn",fn); 
fp=fopen(realfn,"w"); 
if(fp==-1||fp==0)return 0; 
fprintf(fp,"%s ",version); 
for(n=0;n<N;n++) 
{ 
fprintf(fp,",%d,%c",notes[n].num,notes[n].length); 
} 
fclose(fp); 
return 1; 
} 



#endif 
/*end of edit.h*/ 